package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.model.ExchangeDataModel;
	
	import flash.display.BitmapData;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/4/18
	 */ 
	public class SaveSuccesWinMediator extends Mediator
	{
		[Inject]
		public var saveRoom:SaveSuccessTitleWindow;
		
		[Inject]
		public var roomModel:ExchangeDataModel;
		
		override public function onRegister():void
		{
			saveRoom.roomImg = new BitmapData(getImg());
			saveRoom.roomImg.width ;
		}
		
		private function getImg():BitmapData
		{
			var minX:Number = 10000;
			var minY:Number = 10000;
			var maxX:Number = -1;
			var maxY:Number = -1;
			var target:UIComponent = roomModel.gridContainer;
			var boundingBox:Rectangle;
			for (var i:int = 0; i < roomModel.gridContainer.numChildren; i++)
			{
				if ((roomModel.gridContainer.getChildAt(i) is AbstractDrawingRoom) && !(roomModel.gridContainer.getChildAt(i) is IndependentFloor))
				{
					var host:AbstractDrawingRoom = roomModel.gridContainer.getChildAt(i) as AbstractDrawingRoom;
					boundingBox = host.getRect(host.parent);
					if (boundingBox.x <= minX)
						minX = boundingBox.x;
					if (boundingBox.y <= minY)
						minY = boundingBox.y;
					if (boundingBox.x + boundingBox.width >= maxX)
						maxX = boundingBox.x + boundingBox.width;
					if (boundingBox.y + boundingBox.height >= maxY)
						maxY = boundingBox.y + boundingBox.height;
				}					
			}
			var matrix:Matrix = new Matrix();
			matrix.translate(-minX + 32, -minY + 32);
			var bitmapData:BitmapData = new BitmapData(maxX - minX + 64, maxY - minY + 64);
			bitmapData.draw(target,matrix); 
			return bitmapData;
		}
	}
}